package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
@Transactional
public class WmSensitiveServiceImpl implements WmSensitiveService {
    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    //新增
    @Override
    public ResponseResult insert(WmSensitive wmSensitive) {
        if (wmSensitive == null || StringUtils.isBlank(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //新增的敏感词不能重复
        String sensitives = wmSensitive.getSensitives();
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getSensitives, sensitives);
        Integer count = wmSensitiveMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "新增的敏感词不能重复");
        }

        wmSensitive.setCreatedTime(new Date());
        int insertResult = wmSensitiveMapper.insert(wmSensitive);

        return insertResult > 0
                ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult findByPage(SensitiveDto sensitiveDto) {
        if (sensitiveDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        String sensitives = sensitiveDto.getName();
        Integer currentPage = sensitiveDto.getPage();
        Integer size = sensitiveDto.getSize();
        currentPage = currentPage == null ? 1 : currentPage;
        size = size == null ? 10 : size;

        //模糊查询
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(sensitives), WmSensitive::getSensitives, sensitives);

        //创建时间倒序
        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);

        //构建分页查询
        Page<WmSensitive> page = new Page<>(currentPage, size);

        page = wmSensitiveMapper.selectPage(page, queryWrapper);
        Long total = page.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(currentPage, size, total.intValue());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult update(WmSensitive wmSensitive) {
        //入参判空
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        String sensitives = wmSensitive.getSensitives();
        if (StringUtils.isBlank(sensitives)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词不能修改为空");
        }

        //去重
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getSensitives, sensitives);
        Integer count = wmSensitiveMapper.selectCount(queryWrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词重复");
        }

        int updateResult = wmSensitiveMapper.updateById(wmSensitive);

        return updateResult > 0
                ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //入参判空
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.查询敏感词
        WmSensitive wmSensitive = wmSensitiveMapper.selectById(id);
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //3.删除
        int deleteResult = wmSensitiveMapper.deleteById(id);

        return deleteResult > 0
                ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS)
                : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}
